use crate::{app_writer::AppResult, config::CFG};
use sqlx::PgPool;
use tokio::sync::OnceCell;
use tracing::info;
pub static DB: OnceCell<PgPool> = OnceCell::const_new();
pub async fn init_db_conn() {
    info!("Initiating database connection");
    DB.get_or_init(|| async {
        PgPool::connect(&CFG.database.database_url)
            .await
            .expect("数据库连接失败。")
    })
    .await;
    info!("Initiated database connection");
}

pub fn db_get() -> AppResult<&'static sqlx::PgPool> {
    let db = DB.get().ok_or(anyhow::anyhow!("数据库连接失败。"))?;
    Ok(db)
}